import { createStore } from 'vuex';

export default createStore({
  state: {
    tags: [{
      path: '/admin/index',
      title: '首页',
      active: true
    }],
    inputVisible: false,
    inputValue: '',
    activeTab: null,
  },
  mutations: {
    ACTIVATE_TAG(state, tagPath) {
      const existingTag = state.tags.find(tag => tag.path === tagPath);
      if (existingTag) {
        // 重置所有标签的激活状态  
        state.tags.forEach(tag => {
          tag.active = false;
        });
        // 激活找到的标签  
        existingTag.active = true;
      }

    },
    ADD_TAG(state, tag) {
      if (!state.tags.find(existingTag => existingTag.path === tag.path)) {
        state.tags.push(tag);
      }
    },
    REMOVE_TAG(state, tag) {
      const index = state.tags.findIndex(t => t.path === tag.path);
      if (index !== -1) {
        state.tags.splice(index, 1);
        // 如果移除的是当前激活的标签，你可能还需要设置一个新的当前激活标签  
      }
    },
    SET_INPUT_VISIBLE(state, visible) {
      state.inputVisible = visible;
    },
    SET_INPUT_VALUE(state, value) {
      state.inputValue = value;
    },
  },
  actions: {
    addTag({ commit }, tag) {
      commit('ADD_TAG', tag);
    },
    removeTag({ commit }, tag) {
      commit('REMOVE_TAG', tag);
    },
    setInputVisible({ commit }, visible) {
      commit('SET_INPUT_VISIBLE', visible);
    },
    setInputValue({ commit }, value) {
      commit('SET_INPUT_VALUE', value);
    },
  },
  getters: {
    tags: state => state.tags,
    inputVisible: state => state.inputVisible,
    inputValue: state => state.inputValue
  }
});